package com.lanou.fooddemo.view;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.lanou.fooddemo.R;
import com.lanou.fooddemo.external.smart_image_view.SmartImageView;
import com.lanou.fooddemo.view.transformer.DepthPageTransformer;

import java.util.ArrayList;
import java.util.List;


/**自定义Gallery
 * Created by LiuYan on 2015/8/14.
 */
public class GalleryImageView extends FrameLayout {
    /**
     * 画廊图片
     */
    private ViewPager viewPager;
    /**
     * 画廊显示数
     */
    private TextView textView;
    /**
     * Gallery的图片填充数据
     */
    private List<String> imageURLData;
    /**
     * 单张url存储变量
     */
    private String imageURL;
    private GalleryImageViewAdapter adapter;
    private List<View> smartImageViews;
    public GalleryImageView(Context context) {
        super(context, null);
    }

    public GalleryImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_gallery_smart_image, this, true);
        viewPager = (ViewPager) findViewById(R.id.galleryViewPager);
        textView = (TextView) findViewById(R.id.galleryTextView);
        imageURLData = new ArrayList<String>();
        smartImageViews = new ArrayList<View>();

        //设置viewPager滑动效果
        setViewPageTransformer();
        //设置Adapter
        setViewPagerAdapter();
        //设置选定page监听
        setViewPagerOnPagerListener();
    }

    /**
     * 设置viewPager滑动效果
     */
    private void setViewPageTransformer() {
        viewPager.setPageTransformer(true, new DepthPageTransformer());
    }

    /**
     * 设置Adapter
     */
    private void setViewPagerAdapter() {
        adapter = new GalleryImageViewAdapter();
        viewPager.setAdapter(adapter);
    }

    /**
     * 设置OnPagerListener
     */
    private void setViewPagerOnPagerListener() {
        viewPager.addOnPageChangeListener(new GalleryImagePageChangeListener());
    }

    /**
     * 添加所有图片的url
     * @param urls 图片url List
     */
    public void addAllOfURLData(List<String> urls) {
        //将url列表全部添加到数据源
        imageURLData.clear();
        imageURLData = urls;
        smartImageViews = new ArrayList<View>();
        for (String url : urls) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.view_gallert_smart_image_item, null);
            SmartImageView imageView = (SmartImageView) view.findViewById(R.id.galleryImageView);
            imageView.setImageUrl(url);
            smartImageViews.add(view);
        }
        adapter = new GalleryImageViewAdapter();
        viewPager.setAdapter(adapter);
        textView.setText("1/" + imageURLData.size());
        textView.setVisibility(VISIBLE);
    }

    /**
     * 加入单张图片url
     * @param url
     */
    public void addOnlyOneOfURLData(String url) {
        imageURL = url;
        smartImageViews = new ArrayList<View>();
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_gallert_smart_image_item, null);
        SmartImageView imageView = (SmartImageView) view.findViewById(R.id.galleryImageView);
        imageView.setImageUrl(url);
        smartImageViews.add(view);
        adapter = new GalleryImageViewAdapter();
        viewPager.setAdapter(adapter);
        textView.setVisibility(INVISIBLE);
    }
    /**
     * 自定义Adapter
     */
    private class GalleryImageViewAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return smartImageViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return (view == o);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(smartImageViews.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = smartImageViews.get(position);
            container.addView(view);
            return view;
        }


    }

    /**
     * 自定义OnPageChangeListener
     */
    private class GalleryImagePageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int i) {
            //构造显示数量字符串
            String number = (i + 1) + "/" + imageURLData.size();
            //设置显示
            textView.setText(number);
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    }
}
